package mission3;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
/**
 * Class used to read or write strings in files
 * 
 */
public class IOManager {

	/**
	 * lecture du fichier MPS
	 * @param path of the file to read
	 */
	public static Iterator<String> read(String path) throws IOException{

		// ArrayList qui contiendra une cellule par ligne du fichier
		ArrayList<String> lineArray = new ArrayList<String>();
		
		FileInputStream fileInput = new FileInputStream(path);
		InputStreamReader fileReader = new InputStreamReader(fileInput);
		BufferedReader buffer = new BufferedReader(fileReader);
		String line; // Variable temporaire stockant la ligne
		while ((line = buffer.readLine()) != null) {
			lineArray.add(line);
		}
		buffer.close();
		fileReader.close();
		fileInput.close();
		return lineArray.iterator(); // Retourne le tableau de ligne(s)
	}


	/**
	 * Ecriture du fichier de sortie
	 * @param path of the file to write and the string to write
	 */ 
	public static void write(String chaine,String path) throws IOException{

		FileWriter fw = new FileWriter (path);
		BufferedWriter bw = new BufferedWriter (fw);
		PrintWriter fichierSortie = new PrintWriter (bw); 
		fichierSortie.print (chaine); 
		fichierSortie.close();
		bw.close();
		fw.close();

	}
}
